package com.atguigu.controller;

import com.atguigu.result.RetVal;
import com.atguigu.search.Product;
import com.atguigu.search.SearchParam;
import com.atguigu.search.SearchResponseVo;
import com.atguigu.service.SearchIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/8/31 16:35
 * @description：es搜索控制器
 * @modified By：
 * @version: 0.0.1
 */
@RestController
@RequestMapping("/search")
public class SearchIndexController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private SearchIndexService searchIndexService;

    //1.创建索引
    @GetMapping("createIndex")
    public RetVal createIndex() {
        restTemplate.createIndex(Product.class);
        restTemplate.putMapping(Product.class);
        return RetVal.ok();
    }

    //2.商品上架增加
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId) {
        searchIndexService.onSale(skuId);
        return RetVal.ok();
    }

    //3.商品下架删除
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId) {
        searchIndexService.offSale(skuId);
        return RetVal.ok();
    }

    //4.商品的热度排名
    @GetMapping("incrHotScore/{skuId}")
    public RetVal incrHotScore(@PathVariable Long skuId) {
        searchIndexService.incrHotScore(skuId);
        return RetVal.ok();
    }

    //5.商品搜索
    @PostMapping
    public RetVal searchProduct(@RequestBody SearchParam searchParam) {
        SearchResponseVo searchResponseVo = searchIndexService.searchProduct(searchParam);
        return RetVal.ok(searchResponseVo);
    }
}
